<template>
  <div class="app-container">
    <el-row :gutter="24">
      <el-col :offset="22">
        <el-button type="primary" @click="handleAdd">添加海外商家</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      style="margin-top: 20px"
      :data="list"
      size="mini"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        label="ID"
        width="50"
        align="center"
      />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image style="width: 20px; height: 20px" :src="scope.row.img" lazy :preview-src-list="[scope.row.img]" />
        </template>
      </el-table-column>
      <el-table-column label="跳转" align="center" prop="url">
        <template slot-scope="scope">
          <el-link :href="scope.row.url">查看<i class="el-icon-view el-icon--right" /></el-link>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column class-name="status-col" label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            @click="handleEdit(scope.row)"
          >编辑
          </el-button>
          <el-button
            type="danger"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      :hide-on-single-page="page.singlePage"
      :current-page.sync="page.current"
      :page-sizes="[5,10,20,50]"
      :page-size="page.size"
      layout="total,sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <el-form label-position="top">
        <el-form-item label="海外商家标题" :label-width="formLabelWidth">
          <el-input v-model="manager.title" autocomplete="off" placeholder="请输入海外商家标题"/>
        </el-form-item>
        <el-form-item label="海外商家图片" :label-width="formLabelWidth">
          <avatar-upload :size="50" :src="manager.img" path="upload/" @imageUrl="getUpLoadImg($event)" />
        </el-form-item>
        <el-form-item label="海外商家跳转" :label-width="formLabelWidth">
          <el-input v-model="manager.url" autocomplete="off" placeholder="请输入海外商家跳转" />
        </el-form-item>
        <el-form-item label="海外商家分类" :label-width="formLabelWidth">
          <template>
            <el-select
              v-model="manager.category"
              filterable
              allow-create
              default-first-option
              placeholder="请选择海外商家分类"
            >
              <el-option
                v-for="item in options"
                :key="item.category"
                :label="item.category"
                :value="item.category"
              />
            </el-select>
          </template>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleNewSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { merchantList, merchantEdit, merchantAdd, merchantDel, merchantCategoryList } from '@/api/admin'
import avatarUpload from '@/components/AvatarUpload'
export default {
  components: {
    avatarUpload
  },
  data() {
    return {
      list: [],
      listLoading: true,
      centerDialogVisible: false,
      formLabelWidth: '80px',
      dialogTitle: '新增海外商家',
      manager: {
        id: -1,
        title: '',
        img: '',
        url: '',
        category: ''
      },
      page: {
        size: 10,
        current: 1,
        total: 1,
        singlePage: true
      },
      options: []
    }
  },
  created() {
    this.fetchData()
    merchantCategoryList().then(res => {
      this.options = res.body
    })
  },
  methods: {
    itemDetail(item) {
      this.$router.push({ path: '/school/class', query: { school: item.id }})
    },
    fetchData() {
      this.listLoading = true
      merchantList(this.page).then(response => {
        this.list = response.body
        this.page.total = response.count
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dialogTitle = '新增海外商家'
      this.manager.id = -1
      this.manager.title = ''
      this.manager.img = ''
      this.manager.url = ''
      this.manager.category = ''
      this.centerDialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑海外商家'
      this.manager.id = row.id
      this.manager.title = row.title
      this.manager.img = row.img
      this.manager.url = row.url
      this.manager.category = row.category
      this.centerDialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该海外商家', '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        merchantDel({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleNewSave() {
      if (this.manager.id === -1) {
        merchantAdd(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      } else {
        merchantEdit(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家编辑成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    // 获取上传图片的路径
    getUpLoadImg(val) {
      this.manager.img = val
    }
  }
}
</script>
